//
//  CommentHelper.m
//  carcareIOS
//
//  Created by ileo on 15/8/19.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "CommentHelper.h"
#import "SystemHelper.h"
#import "MWFeedBackVC.h"
#import "WRGestureViewController.h"
@implementation AIComment

@end

@implementation TriggerComment

-(void)triggerByVersionUpdate{
    [[AIComment alloc] init].isCommented = NO;
}

@end

@interface CommentHelper()

@property (nonatomic, strong) AIComment *aiComment;
@property (nonatomic, assign) NSInteger seconds;
@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) NSMutableArray *seePages;
@property (nonatomic, strong) NSMutableDictionary *useFunctions;

@end

@implementation CommentHelper

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.aiComment = [[AIComment alloc] init];
        self.seconds = 0;
        __weak __typeof(self) wself = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 block:^{
            [wself checkTime];
        } repeats:YES];
        [self.timer setFireDate:[NSDate distantFuture]];
        
        self.seePages = [NSMutableArray arrayWithCapacity:4];
        self.useFunctions = [NSMutableDictionary dictionaryWithCapacity:6];

    }
    return self;
}

-(void)checkTime{

    if (self.aiComment.isCommented) {
        [self.timer setFireDate:[NSDate distantFuture]];
        return;
    }
    
    self.seconds++;
    if (self.seconds > 600) {
        [self showCommentTips];
    }
}

-(void)showCommentTips{
    self.aiComment.isCommented = YES;
    __weak __typeof(self) wself = self;
    TipsView *tips = [TipsView commentTipsWithTitle:@"" lightTitles:@[@"喜欢新版本的车挣吗?",@"如果还行,就为我点个赞吧!"] cancelClick:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    }];
    [tips addLeftButton:@"我要吐槽" clickLeft:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
        [wself goCommentPage];
    } rightButton:@"鼓励一下" clickRight:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
        [CommentHelper goAppStore];
    }];
    [AlertMgr showView:tips];
}
//跳转意见反馈页面
- (void)goCommentPage
{
    MWFeedBackVC * vc = [[MWFeedBackVC alloc] initWithMainViewType:TYPE_MAIN_VIEW_SCROLL_NORMAL];
    UINavigationController * vcNav = [UINavigationController navigationControllerWithRootViewController:vc];
    UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
    [vc navSetupLeftImageName:@"close_icon" action:^{
        [vcNav.view removeFromSuperview];
    }];
    
    [mainWindow addSubview:vcNav.view];
//    [SCREEN.currentViewController presentViewController:[UINavigationController navigationControllerWithRootViewController:vc] animated:YES completion:nil];
}
-(void)useFunction:(NSString *)funcion{
    NSInteger value = [self.useFunctions[funcion] integerValue];
    value++;
    self.useFunctions[funcion] = @(value);
    if (value >= 3) {
        [self showCommentTips];
    }
}

-(void)seePage:(NSString *)page{
    if (![self.seePages containsObject:page]) {
        [self.seePages addObject:page];
        if (self.seePages.count == 4) {
            [self showCommentTips];
        }
    }
}


+(void)getFirstReward{
    if (![CommentHelper sharedInstance].aiComment.isCommented){
        [[CommentHelper sharedInstance] showCommentTips];
    }
}

+(void)goAppStore{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:APP_STORE]];
}

+(void)useFunction:(Use_function)function{
    if (![CommentHelper sharedInstance].aiComment.isCommented){
        [[CommentHelper sharedInstance] useFunction:[NSString stringWithFormat:@"%zd",function]];
    }
}

+(void)seePage:(See_Page)page{
    if (![CommentHelper sharedInstance].aiComment.isCommented){
        [[CommentHelper sharedInstance] seePage:[NSString stringWithFormat:@"%zd",page]];
    }
}

+(void)startTime{
    if (![CommentHelper sharedInstance].aiComment.isCommented){
        [[CommentHelper sharedInstance].timer setFireDate:[NSDate date]];
    }
}

+(void)stopTime{
    if (![CommentHelper sharedInstance].aiComment.isCommented){
        [[CommentHelper sharedInstance].timer setFireDate:[NSDate distantFuture]];
    }
}

@end
